<?php
namespace app\api\controller;

use think\Request;
use think\Db;

use app\common\controller\Base;
use app\common\controller\Token;
use app\api\model\Admin as AdminModel;

class User extends Base
{
    public function index(Request $request)
    {
        if (Request::instance()->isPost()) {
          
        }
        $this->responseJson(1002);
    }

    // 后台新增用户
    public function add_save(Request $request)
    {
        if (Request::instance()->isPost()) {
            $account = $request->post('account', null);// 账号
            if (empty($account)) {
                $this->responseJson(2004);
            }
            $name = $request->post('name', null);// 名字
            if (empty($account)) {
                $this->responseJson(2005);
            }
            $type = $request->post('type', null);//类型 1、超级管理员，2、管理员 3、暂无
            if (empty($type)) {
                $this->responseJson(2006);
            }
            $tmp = Db::name('admin')->where('account', $account)->find();
            if (!empty($tmp)) {
                $this->responseJson(1010);
            }
            $password = $request->post('password', null);//密码
            if (empty($password)) {
                $this->responseJson(1004);
            }
            $confirmPass = $request->post('confirmPass', null);//确认密码
            if (empty($confirmPass)) {
                $this->responseJson(1008);
            }
            if ($password != $confirmPass) {
                $this->responseJson(1009);
            }
            $salt     = salt();
            $password = md5($password . $salt . '$%$^%$&^%&');
            $admin    = new AdminModel();
            $admin->account  = $account;
            $admin->nickname = $name;
            $admin->password = $password;
            $admin->type     = $type;
            $admin->salt     = $salt;
            $admin->status   = 1;
            $admin->add_time = time();
            $data = $admin->save();
            if (empty($data)) {
                $this->responseJson(1011);
            }
            $this->responseJson();
        }
        $this->responseJson(1002);
    }
}
